package com.szholly.pro.watergas_server.ps.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.pro.watergas_server.ps.service.DataAnalysisService;
import com.szholly.pro.watergas_server.ps.service.OnlineMonitorService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/DataAnalysis/*")
public class DataAnalysisController extends BaseController { 

	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	DataAnalysisService analysisService;
	
	@Autowired
	OnlineMonitorService onlineMonitorService;

	/**
	 * 趋势报表
	 * gislin
	 * 2016年4月28日
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/TrendReport")
	public void TrendReport(HttpServletRequest resq, HttpServletResponse res) throws Exception {

		String trendType = RequestUnit.getString("trendType");
		String parmToChecks = RequestUnit.getString("parmToChecks");
		String mn = RequestUnit.getString("mn");
		String startDateStr = RequestUnit.getString("startDate");
		String endDateStr = RequestUnit.getString("endDate");
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date startDate = sf.parse(startDateStr);
		Date endDate = sf.parse(endDateStr);
		
		HashMap<String, Object> list = null;
		
		if(trendType.equals("month")){
			list = analysisService.getMonthTrendReport(mn, startDate, endDate, parmToChecks);
		}
		else if(trendType.equals("year")){
			list = analysisService.getYearTrendReport(mn, startDate, endDate, parmToChecks);
		}
		
		if(list != null){
			JSONObject resultJson = DataUtil.objectToJsonObject(list);
			outString(resultJson.toString());
		}
		outString("");
	}
	
	/**
	 * 环比报表
	 * gislin
	 * 2016年4月28日
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/X2XReport")
	public void X2XReport(HttpServletRequest resq, HttpServletResponse res) throws Exception {

		String trendType = RequestUnit.getString("trendType");
		String parmToChecks = RequestUnit.getString("parmToChecks");
		String mn = RequestUnit.getString("mn");
		int startYear = RequestUnit.getInt("startYear");
		int yearLength = RequestUnit.getInt("yearLength");
		String mq = RequestUnit.getString("mq");
		
		HashMap<String, Object> list = null;
		
		if(trendType.equals("month")){
			list = analysisService.getX2XReport(mn, startYear,mq, yearLength, parmToChecks,"monitor_data_month");
		}
		else if(trendType.equals("year")){
			list = analysisService.getX2XReport(mn, startYear,"", yearLength, parmToChecks,"monitor_data_year");
		}
		else if(trendType.equals("quarter")){
			list = analysisService.getX2XReport(mn, startYear,mq, yearLength, parmToChecks,"monitor_data_quarter");
		}
		
		if(list != null){
			JSONObject resultJson = DataUtil.objectToJsonObject(list);
			outString(resultJson.toString());
		}
		outString("");
	}
}
